<style lang="less">
@import "@/styles/table-common.less";
</style> 
<template>
  <div class="search">
    <Card>
        <Row @keydown.enter.native="handleSearch">
            <Form ref="searchForm" :model="searchForm" inline :label-width="150">
            <FormItem label="姓名：">
                <Input
                type="text"
                v-model="searchForm.name"
                clearable
                placeholder="请输入姓名"
                style="width: 200px"
                />
            </FormItem>
            <FormItem label="手机号：">
                <Input
                type="text"
                v-model="searchForm.mobile"
                clearable
                placeholder="请输入手机号"
                style="width: 200px"
                />
            </FormItem>
            <FormItem style="margin-left: -35px" class="br">
                <Button @click="handleSearch" type="primary" icon="ios-search"
                >搜索</Button
                >
                <Button @click="handleReset">重置</Button>
            </FormItem>
            </Form>
        </Row>
      <Row class="operation">
        <!-- <Button type="primary" icon="md-arrow-up">导出</Button> -->
        <Button @click="add" type="primary" icon="md-add">添加</Button>
      </Row>
      <Table
        :loading="loading"
        border
        :columns="columns"
        :data="data"
        sortable="custom"
        ref="table"
      >
      </Table>
      <Row type="flex" justify="end" class="page">
        <Page
          :current="searchForm.pageNumber"
          :total="total"
          :page-size="searchForm.pageSize"
          @on-change="changePage"
          @on-page-size-change="changePageSize"
          :page-size-opts="[10, 20, 50]"
          size="small"
          show-total
          show-elevator
          show-sizer
        ></Page>
      </Row>
      
    </Card>
    <addEdit
      :data="form"
      :type="showType"
      :companyId="companyId"
      v-model="showUser"
      @on-submit="getDataList"
    />
  </div>
</template>

<script>
import {
  // getScCompanyById,
  getScCompanyUserBillStatistic,
  scUserUpdStatusById,
  scCompanyKickOut
} from "@/api/index";


import addEdit from "./addEdit.vue";
export default {
  name: "company-info",
  components: {
    addEdit,
  },
  props: ["companyId"],
  data() {
    return {
      message: "",
      height: 510,
      showUser: false,
      showType: "0",
      loading: true,
      searchForm: {
        pageNumber: 1,
        pageSize: 10,
        // sort: "createTime",
        order: "desc",
      },
      columns: [
        {
          title: "姓名",
          key: "realName",
          minWidth: 220,
          align: "center",
          render: (h, params) => {
            let nodeTxt = h(
              "a",
              {
                on: {
                  click: () => {
                    let obj = {
                      ...params.row,
                      companyId: this.companyId,
                      companyName: this.companyInfo.companyName
                    }
                    this.$router.push({name: 'person_info',params: obj})
                  }
                }
              },
              params.row.realName
            )
            return h('div', [nodeTxt])
          }
        },
        {
          title: "手机号",
          key: "mobile",
          width: 220,
          align: "center"
        },
        {
          title: "创建时间",
          key: "createTime",
          minWidth: 220,
          align: "center"
        },
        {
          title: "启用状态",
          key: "status",
          width: 140,
          align: "center",
          render: (h, params) => {
            if (params.row.status == 0) {
              return h("div", [
                h("Badge", {
                  props: {
                    status: "success",
                    text: "启用",
                  },
                }),
              ]);
            } else if (params.row.status == -1) {
              return h("div", [
                h("Badge", {
                  props: {
                    status: "error",
                    text: "禁用",
                  },
                }),
              ]);
            }
          },
          // filters: [
          //   {
          //     label: "启用",
          //     value: 0,
          //   },
          //   {
          //     label: "禁用",
          //     value: -1,
          //   },
          // ],
          // filterMultiple: false,
          filterRemote: (e) => {
            let v = "";
            if (e.length > 0) {
              v = e[0];
            }
            this.searchForm.status = v;
            this.searchForm.pageNumber = 1;
            this.getDataList();
          },
        },
        {
          title: "操作",
          key: "createTime",
          width: 130,
          align: "center",
          fixed: "right",
          render: (h, params) => {
            let enableOrDisable = "";
            if (params.row.status == 0) {
              enableOrDisable = h(
                "a",
                {
                  on: {
                    click: () => {
                      this.chageable(params.row, -1);
                    },
                  },
                },
                "禁用"
              );
            } else {
              enableOrDisable = h(
                "a",
                {
                  on: {
                    click: () => {
                      this.chageable(params.row, 0);
                    },
                  },
                },
                "启用"
              );
            }
            return h("div", [
              enableOrDisable,
              h("Divider", {
                props: {
                  type: "vertical",
                },
              }),
              h(
                "a",
                {
                  on: {
                    click: () => {
                      this.remove(params.row)
                    },
                  },
                },
                "踢出"
              )
            ]);
          },
        },
      ],
      data: [],
      companyInfo: {},
      adminInfo: {},
    };
  },
  methods: {
    init() {
      this.getDataList();
      // this.getScCompanyByIdData()
    },
    changeSort(e) {
      this.searchForm.sort = e.key;
      this.searchForm.order = e.order;
      if (e.order == "normal") {
        this.searchForm.order = "";
      }
      this.getDataList();
    },
    getDataList() {
      // 多条件搜索用户列表
      this.loading = true;
      getScCompanyUserBillStatistic({companyId:this.companyId,...this.searchForm}).then(res => {
        this.loading = false
        if (res.success) {
          // this.data = res.result
          this.data = res.result.records;
          this.total = res.result.totalElements;
          if (this.data.length == 0 && this.searchForm.pageNumber > 1) {
            this.searchForm.pageNumber -= 1;
            this.getDataList();
          }
        }
      })
    },
    add() {
      this.showType = "2";
      this.showUser = true;
    },
    changePage(v) {
      this.searchForm.pageNumber = v;
      this.getDataList();
    },
    changePageSize(v) {
      this.searchForm.pageSize = v;
      this.getDataList();
    },
    chageable(v, status) {
      let statusTxt = status == 0 ? '启用' : '禁用'
      this.$Modal.confirm({
        title: `确认${statusTxt}`,
        content: `您确认要${statusTxt}` + v.realName + " ?",
        loading: true,
        onOk: () => {
          scUserUpdStatusById({id:v.scuid, status}).then((res) => {
            this.$Modal.remove();
            if (res.success) {
              this.$Message.success("操作成功");
              this.getDataList();
            }
          });
        },
      });
    },
    remove(v) {
      let statusTxt = '踢出'
      this.$Modal.confirm({
        title: `确认${statusTxt}`,
        content: `您确认要${statusTxt}` + v.realName + " ?",
        loading: true,
        onOk: () => {
          scCompanyKickOut({id: this.companyId, uid: v.scuid}).then((res) => {
            this.$Modal.remove();
            if (res.success) {
              this.$Message.success("操作成功");
              this.getDataList();
            }
          });
        },
      })
    },
    handleSearch() {
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = 10;
      this.getDataList();
    },
    handleReset() {
      this.searchForm.name = '';
      this.searchForm.mobile = '';
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = 10;
      // 重新加载数据
      this.getDataList();
    },
    
  },
  created() {
   
    this.init()
  },
  mounted() {
    // 计算高度
    this.height = Number(document.documentElement.clientHeight - 230);
    
  },
  // activated() {
  //   console.log(1111111111111)
  //   if(Object.keys(this.$route.params).length>0) {
  //     this.companyInfo = this.$route.params
  //     this.adminInfo = this.companyInfo.user || {}
  //     this.companyId = this.companyInfo.id
  //     localStorage.setItem('localStorage_companyInfo',JSON.stringify(this.$route.params))
  //   }else{
  //     this.companyInfo = JSON.parse(localStorage.getItem('localStorage_companyInfo'))
  //     this.adminInfo = this.companyInfo.user || {}
  //     this.companyId = this.companyInfo.id
  //   }
  //   this.init()
  // }
};
</script>